package com.jia.leetCode;

import java.util.Arrays;
import java.util.HashMap;

public class Pro1331 {
    public int[] arrayRankTransform(int[] arr) {
        if(arr.length == 0) { return arr; }
        int[] tmp = new int[arr.length];
        System.arraycopy(arr, 0, tmp, 0, arr.length);
        Arrays.sort(tmp);
        HashMap<Integer,Integer> map = new HashMap<>();
        int rank = 1;
        for(int i = 0; i < tmp.length; i ++) {
            if(i == 0) {
                map.put(tmp[i], rank);
                rank++;
            }else if(i != 0) {
                if(tmp[i-1] != tmp[i]) {
                    map.put(tmp[i], rank);
                    rank++;
                }
            }
        }


        for(int i = 0; i < arr.length; i++) {
            arr[i] = map.get(arr[i]);
        }
        return arr;
    }

    public static void main(String[] args) {
        Pro1331 pro1331 = new Pro1331();
        int[] arr = {40,10,20,30};
        System.out.println(pro1331.arrayRankTransform(arr));;
    }
}
